package com.qingcheng.controller;

import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.pojo.user.User;
import com.qingcheng.service.OrderServiceFeign;
import com.qingcheng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<User> findPage(int page, int size){
        return userService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<User> findList(@RequestBody Map<String,Object> searchMap){
        return userService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<User> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  userService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public User findById(String username){
        return userService.findById(username);
    }


    @PostMapping("/add")
    public Result add(@RequestBody User user){
        userService.add(user);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user){
        userService.update(user);
        return new Result();
    }

    @GetMapping("/delete")
    public Result delete(String username){
        userService.delete(username);
        return new Result();
    }

    //根据username查询订单
    @Resource
    private OrderServiceFeign orderServiceFeign;

    @PostMapping("/selectOrderList")
    public List<Order> selectOrderList(String username){
        return orderServiceFeign.selectOrderList(username);
    }

    @GetMapping("/sendMsg")
    public Result sendMsg(String phone){
        userService.sendMsg(phone);
        return new Result(200,"发送成功","");
    }

    @PostMapping("/register")
    public Result save(@RequestBody User user,String smsCode){
        userService.register(user,smsCode);
        return new Result(200,"注册成功","");
    }

    // 登录
    @PostMapping("/login")
    public Result loginUser(@RequestBody User user) {
        // 调用service方法实现登录
        // 返回token值，使用jwt生成
        String token = userService.login(user);
        return new Result(200, "查询成功", token);
    }

    // 根据token获取用户信息
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestParam String username) {
        // 查询数据库根据用户id获取用户信息
        User user = userService.findById(username);
        return new Result(200, "查询成功", user);
    }

}
